var express = require('express');
var router = express.Router();
const path = require('path');
const fs = require('fs');
// sql
const query = require('../mysql');

router.get('/', async (req, res) => {
	let name = req.query.name;

	if (name) {
		let sqlStr = `select * from person where name like ?`;
		let result = await query(sqlStr, [`%${name}%`]);
		if (result.length == 0) {
			return res.send({ status: 100, message: '无用户' });
		}
		res.send({ status: 200, message: '获取用户信息成功', result });
		return;
	}
	let sqlStr = `select * from person`;
	let result = await query(sqlStr);
	if (result.length == 0) {
		return res.send({ status: 100, message: '无用户' });
	}
	res.send({ status: 100, message: '获取用户信息成功', result });
});

// 获取各类信息
router.get('/msg', async (req, res) => {
	let { name } = req.query;
	// console.log(name, collage);
	// award 获奖
	let sqlStr0 = `select name from award where awardee like ?`,
		result0 = await query(sqlStr0, [`%${name}%`]);
	// essay 论文
	let sqlStr1 = `select name from essay where author like ?`,
		result1 = await query(sqlStr1, [`%${name}%`]);
	// patent 专利
	let sqlStr2 = `select name from patent where inventor like ?`,
		result2 = await query(sqlStr2, [`%${name}%`]);
	// project 项目
	let sqlStr3 = `select name from project where commander like ?`,
		result3 = await query(sqlStr3, [`%${name}%`]);
	// work 著作与教材
	let sqlStr4 = `select name from work where author like ?`,
		result4 = await query(sqlStr4, [`%${name}%`]);
	let resArr = [
		{ name: '获奖', data: result0, length: result0.length },
		{ name: '论文', data: result1, length: result1.length },
		{ name: '专利', data: result2, length: result2.length },
		{ name: '项目', data: result3, length: result3.length },
		{ name: '著作', data: result4, length: result4.length },
	];

	res.send({ status: 200, data: resArr });
});

module.exports = router;
